package designpattern.util;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

/**
 * XML工具类
 */
public class XMLUtil {
    /**
     * 根据xml中的节点名字返回内容
     * @param domName 节点名字
     * @return 节点内的字符串内容
     */
    public static String getXMLContent(String domName){
        File file = new File("src/designpattern/config.xml");
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder documentBuilder = null;
        String classPath=null;
        try {
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(file);
            classPath = document.getElementsByTagName(domName).item(0).getTextContent();
        }
        catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return classPath;
    }
}
